// Copyright (c) Lawrence Livermore National Security, LLC and other VisIt
// Project developers.  See the top-level LICENSE file for dates and other
// details.  No copyright assignment is required to contribute to VisIt.

#include <ViewerConnectionProgressDialog.h>
#include <QApplication>
#include <QLabel>
#include <QLayout>
#include <QPushButton>
#include <QRadioButton>
#include <QRect>
#include <QScreen>
#include <QTimer>
#include <QTransform>

// Icons
#include <leftdesk.xpm>
#include <super.xpm>

// ****************************************************************************
// Method: ViewerConnectionProgressDialog::ViewerConnectionProgressDialog
//
// Purpose: 
//   Constructor for the ViewerConnectionProgressDialog class.
//
// Arguments:
//   component : The name of the component being launched.
//   host      : The name of the computer where the component is launching.
//   par       : Whether or not the component is parallel.
//
// Programmer: Brad Whitlock
// Creation:   Fri Sep 27 14:38:50 PST 2002
//
// Modifications:
//   Brad Whitlock, Wed May 7 10:16:53 PDT 2003
//   I initialized some new flags.
//
//   Brad Whitlock, Fri May 23 11:21:26 PDT 2008
//   Qt 4.
//
// ****************************************************************************

ViewerConnectionProgressDialog::ViewerConnectionProgressDialog() :
    QWidget(0, Qt::Dialog), ViewerConnectionProgress()
{
    setWindowModality(Qt::ApplicationModal);

    iconFrame = 0;
    cancelledShow = false;

    // Create the timer that animates the icons.
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()),
            this, SLOT(updateAnimation()));

    // Add the widgets...
    QVBoxLayout *topLayout = new QVBoxLayout(this);
    topLayout->setContentsMargins(10,10,10,10);
    topLayout->setSpacing(10);

    QHBoxLayout *iconLayout = new QHBoxLayout;
    topLayout->addLayout(iconLayout);
    iconLayout->setSpacing(10);
    QPixmap left(leftdesk_xpm);
    QLabel *leftComputer = new QLabel("", this);
    leftComputer->setPixmap(left);
    iconLayout->addWidget(leftComputer);

    // Create some radio buttons to display the progress.
    QHBoxLayout *dotLayout = new QHBoxLayout;
    iconLayout->addLayout(dotLayout);
    dotLayout->setContentsMargins(0,0,0,0);
    dotLayout->setSpacing(5);
    for(int i = 0; i < 6; ++i)
    {
        rb[i] = new QRadioButton("", this);
        rb[i]->blockSignals(true);
        dotLayout->addWidget(rb[i]);
    }

    rightComputer = new QLabel("", this);
    iconLayout->addWidget(rightComputer);
    iconLayout->addSpacing(20);
    iconLayout->addStretch(5);

    // Add the cancel button.
    cancelButton = new QPushButton(tr("Cancel"), this);
    connect(cancelButton, SIGNAL(clicked()),
            this, SLOT(cancel()));
    iconLayout->addWidget(cancelButton);

    // Add a message string.
    msgLabel = new QLabel("", this);
    topLayout->addWidget(msgLabel);

    // Set parallel to false. We set and unset so the pixmap gets created.
    parallel = true;
    SetParallel(false);

    // Set the component name.
    SetComponentName("component");
}

// ****************************************************************************
// Method: ViewerConnectionProgressDialog::~ViewerConnectionProgressDialog
//
// Purpose: 
//   Destructor for the ViewerConnectionProgressDialog class.
//
// Programmer: Brad Whitlock
// Creation:   Fri Sep 27 14:40:24 PST 2002
//
// Modifications:
//   
// ****************************************************************************

ViewerConnectionProgressDialog::~ViewerConnectionProgressDialog()
{
}

// ****************************************************************************
// Method: ViewerConnectionProgressDialog::setComponentName
//
// Purpose: 
//   Sets the component name so that the window displays the proper messages.
//
// Arguments:
//   cm : The new component name.
//
// Programmer: Brad Whitlock
// Creation:   Wed May 7 10:20:41 PDT 2003
//
// Modifications:
//   Brad Whitlock, Tue Apr 29 13:18:38 PDT 2008
//   Support for internationalization.
//
//   Brad Whitlock, Fri May 23 11:32:17 PDT 2008
//   Qt 4.
//
// ****************************************************************************

void
ViewerConnectionProgressDialog::SetComponentName(const std::string &cn)
{
    componentName = cn;

    // Set the window caption.
    QString str(componentName.c_str());
    str.append(" launch progress");
    setWindowTitle(str);
    updateMessage();
}

// ****************************************************************************
// Method: ViewerConnectionProgressDialog::setHostName
//
// Purpose: 
//   Set a new host name.
//
// Arguments:
//   host  : The new host name.
//
// Programmer: Brad Whitlock
// Creation:   Tue Nov 29 19:50:35 PST 2011
//
// Modifications:
//   
// ****************************************************************************

void
ViewerConnectionProgressDialog::SetHostName(const std::string &host)
{
    hostName = host;
    updateMessage();
}

// ****************************************************************************
// Method: ViewerConnectionProgressDialog::SetParallel
//
// Purpose: 
//   Set whether the component is parallel.
//
// Arguments:
//
// Returns:    
//
// Note:       
//
// Programmer: Brad Whitlock
// Creation:   Tue Nov 29 16:00:00 PST 2011
//
// Modifications:
//   
// ****************************************************************************

void
ViewerConnectionProgressDialog::SetParallel(bool p)
{
    if(parallel != p)
    {
        parallel = p;

        // Create the pixmap for the host that we're connecting to.
        QPixmap right;
        if(parallel)
            right = QPixmap(super_xpm);
        else
        {
            QPixmap left(leftdesk_xpm);
            QTransform t;
            t.scale(-1., 1.);
            right = left.transformed(t);
        }
        rightComputer->setPixmap(right);
    }
}

void
ViewerConnectionProgressDialog::Show()
{
    show();
}

void
ViewerConnectionProgressDialog::Hide()
{
    hide();
}


// ****************************************************************************
// Method: ViewerConnectionProgressDialog::updateMessage
//
// Purpose: 
//   Update the message using the component name and host name.  
//
// Programmer: Brad Whitlock
// Creation:   Tue Nov 29 19:50:06 PST 2011
//
// Modifications:
//   
// ****************************************************************************

void
ViewerConnectionProgressDialog::updateMessage()
{
    // Set the message string.
    QString msg;
    if(parallel)
        msg = tr("VisIt is waiting for a parallel %1 to launch on %2.").
              arg(componentName.c_str()).arg(hostName.c_str());
    else
        msg = tr("VisIt is waiting for a %1 to launch on %2.").
              arg(componentName.c_str()).arg(hostName.c_str());
    msgLabel->setText(msg);
}

// ****************************************************************************
// Method: ViewerConnectionProgressDialog::show
//
// Purpose: 
//   This Qt slot function tells the window to show itself after a delay.
//
// Programmer: Brad Whitlock
// Creation:   Fri Sep 27 14:40:45 PST 2002
//
// Modifications:
//   
// ****************************************************************************

void
ViewerConnectionProgressDialog::show()
{
    iconFrame = 0;
    cancelled = false;
    cancelledShow = false;

    // Start the timer so the event loop has something to do and is not
    // obligated to process the single shot timer show event.
    timer->start(300);

    // Show the dialog a few seconds after this method is called. This way
    // the window will not pop-up for fast launching processes. If the timeout
    // is zero, don't bother with a timer.
    if(timeout == 0)
        timedShow();
    else
        QTimer::singleShot(timeout, this, SLOT(timedShow()));
}

// ****************************************************************************
// Method: ViewerConnectionProgressDialog::hide
//
// Purpose: 
//   This Qt slot function hides the dialog.
//
// Programmer: Brad Whitlock
// Creation:   Fri Sep 27 14:41:28 PST 2002
//
// Modifications:
//   
// ****************************************************************************

void
ViewerConnectionProgressDialog::hide()
{
    cancelledShow = true;
    timer->stop();
    QWidget::hide();
}

// ****************************************************************************
// Method: ViewerConnectionProgressDialog::timedShow
//
// Purpose: 
//   This Qt slot function shows the dialog after a delay.
//
// Programmer: Brad Whitlock
// Creation:   Fri Sep 27 14:42:10 PST 2002
//
// Modifications:
//   Brad Whitlock, Wed Apr 23 09:03:17 PDT 2003
//   I made the window raise and center iself.
//
//   Brad Whitlock, Fri May 23 11:33:00 PDT 2008
//   Make the window active.
//
//   Kathleen Biagas, Wed Apr  5 13:04:35 PDT 2023
//   Replace obosolete desktop() with primaryScreen().
//
// ****************************************************************************

void
ViewerConnectionProgressDialog::timedShow()
{
    if(!cancelledShow)
    {
        QWidget::show();
        raise();
        
        // Move the window a little above center.
        int w = qApp->primaryScreen()->geometry().width();
        int h = qApp->primaryScreen()->geometry().height();
        int x = (w - width()) / 2;
        int y = (h - height()) / 2 - (height() * 6 / 5);
        move(x, y);

        // Make it the active viewer window.
        activateWindow();
    }
}

// ****************************************************************************
// Method: ViewerConnectionProgressDialog::updateAnimation
//
// Purpose: 
//   This Qt slot function updates the dialog's progress indicators.
//
// Programmer: Brad Whitlock
// Creation:   Fri Sep 27 14:42:50 PST 2002
//
// Modifications:
//   
// ****************************************************************************

void
ViewerConnectionProgressDialog::updateAnimation()
{
    if(isVisible())
    {
        ++iconFrame;
        if(iconFrame > 5)
            iconFrame = 0;

        for(int i = 0; i < 6; ++i)
            rb[i]->setChecked(i == iconFrame);
    }   
}

// ****************************************************************************
// Method: ViewerConnectionProgressDialog::cancel
//
// Purpose: 
//   This Qt slot function sets the cancel flag which is queried later to
//   determine whether or not the launch should be cancelled.
//
// Programmer: Brad Whitlock
// Creation:   Fri Sep 27 14:43:25 PST 2002
//
// Modifications:
//   
// ****************************************************************************

void
ViewerConnectionProgressDialog::cancel()
{
    cancelled = true;
    hide();
}

